#include "LogFile.h"

#include "AppDefine.h"

CLogFile::CLogFile(int iFileMaxSize /*= 5*/, int iFileNameType /*= 0*/)
    : m_iFileMaxSize(iFileMaxSize)
    , m_bIsWriteLog(false)
    , m_iFileNameType(iFileNameType)
{

}

CLogFile::~CLogFile()
{
    m_file.close();
}

void CLogFile::CreateFile(QString strFileName /*= ""*/)
{
    // 获取应用程序的目录路径
    m_strPath = QCoreApplication::applicationDirPath();
    if (strFileName.isEmpty())
    {
        strFileName = GetFileName();
    }
    // 拼接日志文件名
    QString strPathFile =  m_strPath + "/" + strFileName;
    // 设置日志文件路径和文件名
    m_file.setFileName(strPathFile);

    // 如果成功打开日志文件
    if (m_file.open(QIODevice::ReadWrite | QIODevice::Append))
    {
        //创建成功,打印日志文件路径
        printf("open log success %s\n", strPathFile.toStdString().c_str());
        // 返回
        return;
    }

    // 如果成功以只写方式打开日志文件
    if (m_file.open(QIODevice::WriteOnly))
    {
        // 创建成功, 打印日志文件路径
        printf("open log success %s\n", strPathFile.toStdString().c_str());
    }
}

int CLogFile::GetFileSize()
{
    int size = 0;
    size = m_file.size();
    return size;
}

void CLogFile::CloseFile()
{
    m_file.close();
}

void CLogFile::SetIsWriteLog(bool bIsWriteLog)
{
    m_bIsWriteLog = bIsWriteLog;
}

void CLogFile::SetFileMaxSize(int iFileMaxSize)
{
    m_iFileMaxSize = iFileMaxSize;
}

void CLogFile::SetFileNameType(int iFileNameType)
{
    m_iFileNameType = iFileNameType;
}

QString CLogFile::GetFileName()
{
    QString strFileName = QString("../log/%1.log").arg(APP_NAME);
    if (1 == m_iFileNameType)
    {
        strFileName = QString("../log/%1(%2).log").arg(APP_NAME).arg(QDate::currentDate().toString("yyyy"));
    } else if (2 == m_iFileNameType)
    {
        strFileName = QString("../log/%1(%2).log").arg(APP_NAME).arg(QDate::currentDate().toString("yyyyMM"));
    } else if (3 == m_iFileNameType)
    {
        strFileName = QString("../log/%1(%2).log").arg(APP_NAME).arg(QDate::currentDate().toString("yyyyMMdd"));
    } else if (4 == m_iFileNameType)
    {
        strFileName = QString("../log/%1(%2).log").arg(APP_NAME).arg(QDateTime::currentDateTime().toString("yyyyMMdd"));
    } else if (5 == m_iFileNameType)
    {
        strFileName = QString("../log/%1(%2).log").arg(APP_NAME).arg(QDateTime::currentDateTime().toString("yyyyMMdd_HHmm"));
    }

    return strFileName;
}

void CLogFile::WriteLog(const QString& strInfo)
{
    if (strInfo.isEmpty()) return;

    if (!m_file.isOpen()) {
        //日志文件未打开则创建日志文件
        CreateFile();
    }

    // 如果文件大小超过日志最大值,则直接返回
    if (m_file.size() >= m_iFileMaxSize*1024*1024)
    {
        return;
    }

    //打印日志到控制台
    char* pLog = strInfo.toUtf8().data();
    printf(pLog);
    printf("\n");

    if (!m_bIsWriteLog) return; //不写日志则返回

    //上锁以确保日志的写入顺序
    m_mutexLog.lock();

    //获取当前时间和文件名以及行号
    QDateTime now = QDateTime::currentDateTime();
    //将时间和文件名以及行号拼接到日志信息中
    QString str = now.toString("yyyy-MM-dd hh:mm:ss.zzz ");
    str += strInfo + "\r\n";

    // 写入日志到文件中
    m_file.write(str.toUtf8());
    m_file.flush();

    // 解锁
    m_mutexLog.unlock();
}
